
const api = require("../../config/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reportList: [],
    psCustomerCode: '',
    typeOptions: ['类型', '待发布/待派遣', '未接单', '未到场','已到场','检测中','维修中','已结束'], // 下拉选项
    selectedType: '类型',
    workOrderState:-1,
    pageNum:1,
  },

  onMyEstimation: function (e) {
    const reportIndex = e.currentTarget.dataset.index; // 获取当前点击的报告的索引
    const selectedReport = this.data.reportList[reportIndex];
    console.log('selectedReport')
    console.log(selectedReport)
    //页面跳转传参
    wx.navigateTo({
      url: `/pages/MyPage/MyEstimation/MyEstimation?feedbackCode=${selectedReport.feedbackCode}`
    });
  },
  onValue: function (e) {
    wx.navigateTo({
      url: '/pages/MyReport/value/value',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  onShow() {
    //查询
    this.setData({
      selectedType:'类型',
      pageNum:1,
      reportList: [],
      workOrderState:-1,
    })
    this.fetchWorkOrderData();
  },
  fetchWorkOrderData: function (params = {}) {
    wx.getStorage({
      key: 'psCustomerCode',
      success: (res) => {
        this.setData({
          psCustomerCode: res.data
        });
        wx.request({
          url: api.workOrder,
          method: 'POST',
          data: {
            customerCode: this.data.psCustomerCode,
            pageNum: this.data.pageNum,
            pageSize: "5",
            feedbackType: '3',// 3：所有反馈
            workOrderState:this.data.workOrderState,
          },
          success: (res) => {
            if (res.data.code == 200) {
              console.log("我的工单数据：")
              console.log(res.data.data)
              const reportData = res.data.data.rows;
              for (let i = 0; i < reportData.length; i++) {
                reportData[i].stateText = this.getWorkOrderStateText(reportData[i].workOrderState,reportData[i].workOrderType);
                reportData[i].stateClass = this.getWorkOrderStateClass(reportData[i].workOrderState);
              }
              if (reportData.length!=0) {
                this.setData({
                  pageNum: this.data.pageNum + 1,
                });
              }
              console.log('处理后数据：',reportData)
              this.setData({
                reportList: [...this.data.reportList, ...reportData]
              });
            } else {
              wx.showToast({
                title: res.data.mesage,
                icon: "none"
              })
            }
          }
        })
      },
    });
  },
  onWorkOrderDetail: function (e) {
    const reportIndex = e.currentTarget.dataset.index; // 获取当前点击的报告的索引
    const selectedReport = this.data.reportList[reportIndex];
    //页面跳转传参
    wx.navigateTo({
      url: `/pages/MyWorkOrder/orderDetail/orderDetail?workOrderCode=${selectedReport.workOrderCode}`
    });
  },
  // 处理类型选择事件
  onTypeChange: function (e) {
    const index = e.detail.value; // 获取选中的下标
    console.log('index:',index)
    this.setData({
      pageNum:1,
      selectedType: this.data.typeOptions[index] // 更新选中类型
    });
    this.setData({
      workOrderState: index-1,
      reportList: [],
    })
    this.fetchWorkOrderData();
  },
  filterReports: function (selectedType) {
    console.log('根据类型筛选:', selectedType);
  },
  // 获取状态文字
  getWorkOrderStateText(state,type) {
    switch (state) {
      case 0:
        if(type==1)return '待派遣';
        else return '待发布';
      case 1:
        return '未接单';
      case 2:
        return '未到场';
      case 3:
        return '已到场';
      case 4:
        return '检测中';
      case 5:
        return '维修中';
      case 6:
        return '已结束';
      default:
        return '未知';
    }
  },
  // 获取状态对应的样式类名
  getWorkOrderStateClass(state) {
    switch (state) {
      case 0:
        return 'stateToBeRelease'; // 待发布
      case 1:
        return 'statePublished'; // 已发布
      case 2:
        return 'stateNotArrived'; // 未到场
      case 3:
        return 'stateArrived'; // 已到场
      case 4:
        return 'stateInspecting'; // 检测中
      case 5:
        return 'stateRepairing'; // 维修中
      case 6:
        return 'stateFinished'; // 已结束
      default:
        return 'stateUnknown'; // 未知状态
    }
  },
  // 滑到底部
  onScrollToLower: function( ){
    console.log('滑到末尾，刷新')
    this.fetchWorkOrderData()
  },
  // 重新获取数据
  onScrollToUpper: function () {
    console.log('刷新')
    this.resetData()
  },
  resetData: function () {
     //查询
     this.setData({
      selectedType:'类型',
      pageNum:1,
      reportList: [],
      workOrderState:-1,
    })
    this.fetchWorkOrderData();
  }
})